/*
 * Copyright (c) Jack魏 2023 - 2023, All Rights Reserved.
 */

package cn.jack1996.yiyifx.demo.p1;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 按钮代码演示
 *
 * @author Jack魏
 * @since 2023/6/22 14:57
 */
public class ButtonDemo extends Application {
    /**
     * 主入口
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * 开始
     *
     * @param stage 舞台
     */
    @Override
    public void start(Stage stage) {
        stage.setTitle("设置标题");
        // 流式布局：按照控件的添加次序按个摆放，按照从上到下、从左到右的次序摆放。
        FlowPane pane = new FlowPane(10, 10);
        // 居中显示
        pane.setAlignment(Pos.CENTER);
        // 场景
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);
        AtomicInteger num = new AtomicInteger();
        // 标签
        Label label = new Label("初始值：" + num);
        // 按钮
        Button addButton = new Button("加1");
        Button subButton = new Button("减1");
        // 处理按钮事件
        addButton.setOnAction((event) -> label.setText("当前值：" + num.incrementAndGet()));
        subButton.setOnAction((event) -> label.setText("当前值：" + num.decrementAndGet()));
        // 将标签、按钮添加到场景
        pane.getChildren().addAll(addButton, subButton, label);
        stage.show();
    }
}
